package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.BalanceSheet;
import sanrenxing.dayEnd.backRepertoryTotal.service.BalanceSheetBiz;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:每日交易数据金额核算控制器类
 * 编写者：蒋玲苹
 * 日期：2021
 * 版本：1.0
 */
@RestController
@RequestMapping("/BalanceSheet")
public class BalanceSheetController {
    @Autowired
    private BalanceSheetBiz balanceSheetBiz;
    //自动装配  业务处理方法 改
    @Autowired
    private TransactionService transactionService;

    @GetMapping("/balance")
    public Map<String, Object> addBalanceSheet(HttpServletRequest req, int page, int limit) {
        //定义多条件查询对象
        //BalanceSheet balanceSheet = new BalanceSheet();
        BalanceSheet balanceSheet= new BalanceSheet();
        //接收请求中的交易日期
        String tranDate=req.getParameter("tranDate");
        //判断是否加入
        if (tranDate!=null && tranDate.trim().length()!=0) {
            balanceSheet.setTranDate(tranDate);
        }
        //调用业务层多条件查询方法
        //String condition=balanceSheetBiz.getCount(balanceSheet);
        String condition=balanceSheetBiz.getCount(balanceSheet);
        //执行多条件查询语句
        List<BalanceSheet> balanceSheets=balanceSheetBiz.findByPages(limit,page,condition);
        //获取总记录行数
        int totalRows=balanceSheetBiz.count(condition);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        //证券信息securi 改
        map.put("securi",transactionService.ExchangeVarietyRateLXF());
        map.put("msg", "");
        map.put("data",balanceSheets);
        map.put("count", totalRows);
        //返回map
        return map;
    }

    /**
     * 删除数据
     * @param seName
     * @return
     */
    @DeleteMapping("/balance/{seName}")
    public String delete(@PathVariable String seName) {
        //调用业务层代码删除数据
        int rows = balanceSheetBiz.delete(seName);
        //返回
        return rows > 0 ? "success" : "failure";
    }

    /**
     * 批量删除
     * @param seNameStr
     * @return
     */
    @PutMapping("/batchDel")
    public String batchDel(@RequestParam String seNameStr) {
        //对字符串按","分割
        String[] strs=seNameStr.split(",");
        //定义返回值
        String result = "delete success";
        //循环删除
        for (String seName:strs) {
            //调用业务层删除记录
            int rows=balanceSheetBiz.delete(seName);
            //判断
            if (rows < 0) {
                //设置返回信息
                result = "delete failure";
                //中止
                break;
            }
        }
        //返回result
        return result;
    }
}
